package com.library.weidget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.jtjrenren.android.taxi.passenger.R;

/**
 * Created by Administrator on 2016/1/31.
 */
public class CustomeEvaluation extends LinearLayout implements View.OnClickListener {
    private CheckBox cb_1, cb_2, cb_3, cb_4, cb_5;

    public int level = 0;

    public interface ChangeListener {
        void changed(int level);
    }

    private ChangeListener changeListener;

    public void setChangeListener(ChangeListener changeListener) {
        this.changeListener = changeListener;
    }

    public CustomeEvaluation(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }


    private void initView() {
        View.inflate(getContext(), R.layout.custome_evaluation_view, this);

        cb_1 = (CheckBox) findViewById(R.id.custome_evaluation_1);
        cb_2 = (CheckBox) findViewById(R.id.custome_evaluation_2);
        cb_3 = (CheckBox) findViewById(R.id.custome_evaluation_3);
        cb_4 = (CheckBox) findViewById(R.id.custome_evaluation_4);
        cb_5 = (CheckBox) findViewById(R.id.custome_evaluation_5);

        cb_1.setOnClickListener(this);
        cb_2.setOnClickListener(this);
        cb_3.setOnClickListener(this);
        cb_4.setOnClickListener(this);
        cb_5.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.custome_evaluation_1:
                level = 1;
                clear();
                cb_1.setChecked(true);
                break;
            case R.id.custome_evaluation_2:
                level = 2;
                clear();
                cb_1.setChecked(true);
                cb_2.setChecked(true);
                break;
            case R.id.custome_evaluation_3:
                level = 3;
                clear();
                cb_1.setChecked(true);
                cb_2.setChecked(true);
                cb_3.setChecked(true);
                break;
            case R.id.custome_evaluation_4:
                level = 4;
                clear();
                cb_1.setChecked(true);
                cb_2.setChecked(true);
                cb_3.setChecked(true);
                cb_4.setChecked(true);
                break;
            case R.id.custome_evaluation_5:
                level = 5;
                clear();
                cb_1.setChecked(true);
                cb_2.setChecked(true);
                cb_3.setChecked(true);
                cb_4.setChecked(true);
                cb_5.setChecked(true);
                break;
        }
        if (changeListener != null)
            changeListener.changed(level);
    }


    public void setDefault() {
//        cb_1.setChecked(false);
//        cb_2.setChecked(false);
//        cb_3.setChecked(false);
        level = 0;
        if (changeListener != null)
            changeListener.changed(0);
    }

    private void clear() {
        cb_1.setChecked(false);
        cb_2.setChecked(false);
        cb_3.setChecked(false);
        cb_4.setChecked(false);
        cb_5.setChecked(false);
    }
}
